package server

import (
	"common/database/config"
	"context"
	"errors"
	"project_video_srv/internal/models/dao"
	"project_video_srv/internal/models/service"
	pb "project_video_srv/proto"
)

type Server struct {
	pb.UnimplementedVideoServer
}

func (c *Server) Create(ctx context.Context, request *pb.CreateRequest) (*pb.CreateResponse, error) {
	video := dao.Video{
		UserId:       int(request.UserId),
		Video:        request.Video,
		VideoTitle:   request.VideoTitle,
		Address:      request.Address,
		RedPkg:       request.RedPkg,
		Pkg:          request.Pkg,
		Sex:          int(request.Sex),
		Age:          int(request.Age),
		GiveEveryOne: int(request.GiveEveryone),
		Where:        request.Where,
	}
	err := config.DB.Create(&video).Error
	if err != nil {
		return nil, errors.New("视频发布失败")
	}
	return &pb.CreateResponse{Id: int64(video.ID)}, nil
}

func (c *Server) List(ctx context.Context, request *pb.ListRequest) (*pb.ListResponse, error) {
	list := service.List(request)
	var sli []*pb.Videos
	for _, v := range list {
		one := pb.Videos{
			UserId:     int64(v.UserId),
			Video:      v.Video,
			VideoTitle: v.VideoTitle,
			Address:    v.Address,
		}
		sli = append(sli, &one)
	}
	return &pb.ListResponse{Video: sli}, nil
}
